/*
 * FileAsMsg plug-in for Miranda IM and QIP Infium.
 * Sending/receiving files via instant messaging without protocol support.
 * Homepage: http://code.google.com/p/fileasmsg/
 *
 * Copyright (c) 2008, Radist <radist.nt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Name(s) of contributor(s) may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Radist ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Radist BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "TransferWnd.h"
#include "TransferSession.h"
#include "resource.h"
#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif
#include <CommCtrl.h>
#include <wchar.h>
#include "plugin.h"

#define TranslateW_nolpgen TranslateW

extern HINSTANCE hInst;
const char *WndPrefix = "TWnd_";

TransferWnd::TransferWnd(void)
{
}

TransferWnd::TransferWnd( wchar_t *filename, TransferSession *sess )
: session(sess), hDlg(NULL)
{
	if (!session->File.get()) return;
	hDlg = CreateDialogParamW(hInst, MAKEINTRESOURCE(IDD_TRANSFER_DLG), NULL, WndProc, (LPARAM)this);
	if (!hDlg)
	{
		throw LPGENW("Cann't load resource");
	}
	TranslateDialogDefault(hDlg);
	SetDlgItemTextW(hDlg, IDC_FILENAME, filename);
	wchar_t *contact = GetContactName(session->Contact());
	if (contact)
	{
		size_t cl = wcslen(contact), wl = (size_t)GetWindowTextLengthW(hDlg);
		contact = (wchar_t*)_realloc((void*)contact, (cl + wl + 4)*sizeof(wchar_t));
		wcscpy_s(contact + cl, 4, L" - ");
		GetWindowTextW(hDlg, contact + cl + 3, wl + 1);
		SetWindowTextW(hDlg, contact);
		_free(contact);
	}
	LoadWndPos(hDlg, session->Contact(), WndPrefix);
	ShowWindow(hDlg, SW_SHOW);
}

TransferWnd::~TransferWnd(void)
{
	if (hDlg)
	{
		SaveWndPos(hDlg, session->Contact(), WndPrefix);
		DestroyWindow(hDlg);
	}
}

void TransferWnd::CancelTransfer( void )
{
	session->EndTransfer();
}

INT_PTR CALLBACK TransferWnd::WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{

	switch (uMsg)
	{
	case WM_INITDIALOG:
		SetWindowLongW(hWnd, GWL_USERDATA, lParam);
		SendDlgItemMessageW(hWnd, IDC_PROGRESS, PBM_SETRANGE32, 0, ((TransferWnd*)lParam)->session->File->chunks() + 1);
		SendDlgItemMessageW(hWnd, IDC_PROGRESS, PBM_SETPOS, 0, 0);
		SendDlgItemMessageW(hWnd, IDC_PROGRESS, PBM_SETSTEP, 1, 0);
		break;
	case WM_CLOSE:
		TransferWnd *Wnd = (TransferWnd*)GetWindowLong(hWnd, GWL_USERDATA);
		if (IDYES==MessageBoxW(hWnd, TranslateW(L"Are you sure?"), TranslateW(L"Canceling transfer"), MB_YESNO|MB_ICONEXCLAMATION|MB_DEFBUTTON2))
		{
			Wnd->CancelTransfer();
		}
		return TRUE;
	}
	return FALSE;
}

void TransferWnd::Step( void )
{
	if (hDlg) SendDlgItemMessageW(hDlg, IDC_PROGRESS, PBM_STEPIT, 0, 0);
}

void TransferWnd::SetStatus( wchar_t *status, DWORD change_chunks /*= 0*/ )
{
	if (!hDlg) return;
	if (status) SetDlgItemTextW(hDlg, IDC_STATUS, TranslateW_nolpgen(status));
	if (change_chunks)
	{
		SendDlgItemMessageW(hDlg, IDC_PROGRESS, PBM_SETRANGE32, 0, change_chunks);
		SendDlgItemMessageW(hDlg, IDC_PROGRESS, PBM_SETPOS, 0, 0);
	}
}

void TransferWnd::SetIdle( bool _idle )
{
	if (!hDlg) return;
	static wchar_t *idlemsg = TranslateW(L"(idle) ");
	static size_t idlemsglen = wcslen(idlemsg);
	_ASSERTE(idlemsglen<64);
	wchar_t msg[256];
	GetDlgItemTextW(hDlg, IDC_STATUS, msg + idlemsglen, 256 - idlemsglen);
	if (wcsncmp(msg + idlemsglen, idlemsg, idlemsglen))
	{
		if (_idle)
		{
			wmemcpy_s(msg, sizeof(msg)/sizeof(msg[0]), idlemsg, idlemsglen);
			SetDlgItemTextW(hDlg, IDC_STATUS, msg);
		}
	} 
	else
	{
		if (!_idle)
		{
			SetDlgItemTextW(hDlg, IDC_STATUS, msg + 2*idlemsglen);
		}
	}
}

void TransferWnd::Hide(void)
{
	if (hDlg) ShowWindow(hDlg, SW_HIDE);
}

